package cn.juque.lucenecandy.runner;

import cn.juque.lucenecandy.bo.IndexBO;
import cn.juque.lucenecandy.cache.DirectoryCache;
import cn.juque.lucenecandy.cache.IndexInfoCache;
import cn.juque.lucenecandy.cache.IndexReaderCache;
import cn.juque.lucenecandy.cache.IndexWriterCache;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>LuceneCandyExitRunner</li>
 * </ul>
 * @date 2023-12-20
 **/
@Component("luceneCandyExitRunner")
public class LuceneCandyExitRunner implements DisposableBean {

    @Resource
    private IndexInfoCache indexInfoCache;

    @Resource
    private DirectoryCache directoryCache;

    @Resource
    private IndexWriterCache indexWriterCache;

    @Resource
    private IndexReaderCache indexReaderCache;

    @Override
    public void destroy() {
        Map<String, IndexBO> map = this.indexInfoCache.getAll();
        map.forEach((k,v)->{
            this.indexReaderCache.remove(k);
            this.indexWriterCache.remove(k);
            this.indexInfoCache.remove(k);
            this.directoryCache.remove(k);
        });
    }
}
